package org.example.agent.task;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.example.constant.CommonConstant;
import org.example.dto.TaskStatus;
import org.example.enums.*;
import org.example.model.CheckHealthParam;
import org.example.task.HttpTask;
import org.example.task.ITask;
import org.example.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CheckHealthTask extends ParentTask implements ITask {

    private static final Logger LOGGER = LoggerFactory.getLogger(CheckHealthTask.class);

    private CheckHealthParam checkHealthParam;

    private Result<TaskStatus> taskResult;

    public CheckHealthTask(){}

    public CheckHealthTask(String params)
    {
        this.checkHealthParam = JSON.parseObject(HexUtil.decodeHexStr(params, CharsetUtil.CHARSET_UTF_8), CheckHealthParam.class);
    }

    @Override
    public Result<TaskStatus> execute() {
        this.taskResult= Result.ok();
        return taskResult;
    }

    @Override
    public Result<String> reportStatus() {
        this.taskResult = handleResult(this.taskResult, ETaskErrCode.CHECKHEALTH);
        TaskStatus taskStatus = taskResult.getResult();
        taskStatus.setJobId(checkHealthParam.getJobId());
        taskStatus.setTaskId(checkHealthParam.getTaskId());
        taskStatus.setJobType(EJobType.getByJobType(checkHealthParam.getJobType()));
        taskStatus.setTaskStep(EDeployTaskStep.CHECKHEALTH.getStepNum());
        this.taskResult.setResult(taskStatus);
        HttpTask httpTask = new HttpTask(CommonConstant.MASTER_HOST_URL+"admincenter/plan/statereport", JSON.toJSONString(taskResult));
        Result<String> result = httpTask.execute();
        printErrMsg(result, EJobType.DEPLOY, EDeployTaskStep.CHECKHEALTH);
        LOGGER.info("CheckHealthTask reportStatus taskResult={}",taskResult);
        return result;
    }
}
